package com.czxy.servlet;

import cn.itcast.servlet.BaseServlet;
import com.czxy.domain.Cart;
import com.czxy.domain.Product;
import com.czxy.service.ProductService;

import javax.servlet.annotation.WebServlet;

@WebServlet("/cartServlet")
public class CartServlet extends BaseServlet {

    public  String   addCartItem(){

        String pid = getRequest().getParameter("pid");
        String count = getRequest().getParameter("count");


        ProductService service = new ProductService();

        Product product = service.findProductByPid(pid);

        Cart cart = (Cart) getSession().getAttribute("cart");
        if (cart == null){

            cart =  new Cart();
        }


        cart.addCart(product,Integer.parseInt(count));
        getSession().setAttribute("cart",cart);


        return"redirect:/jsp/cart.jsp";



    }



}
